/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Missile.java]
 * Creation Date        [22-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_03_game.tankwar_local.version_5;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;
import java.util.Set;

/**
 * 导弹
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Missile {

	/** 导弹速度 */
	public static final int X_SPEED = 10;
	public static final int Y_SPEED = 10;

	/** 导弹坐标 */
	private int x = 0;
	private int y = 0;

	/** 导弹的宽度和高度 */
	public static final int MISSILE_WIDTH = 5;
	public static final int MISSILE_HEIGHT = 5;

	/** 导弹方向, 借助于坦克的方向 */
	private Direction direction;

	/** 持有MainFrame的引用 */
	private MainFrame client;

	/** 导弹的生命 */
	private boolean live = true;

	/** 敌我导弹区别 */
	private boolean alliance;

	public Missile() {
		super();
	}

	public Missile(int x, int y, boolean alliance, Direction direction,
			MainFrame client) {
		super();
		this.x = x;
		this.y = y;
		this.alliance = alliance;
		this.direction = direction;
		this.client = client;
	}

	/**
	 * 绘制导弹
	 * 
	 * @param g
	 */
	public void paint(Graphics g) {
		// 如果导弹出界或者击中坦克, 从集合中移除
		if (!isLive()) {
			client.missiles.remove(this);
			return;
		}
		// 获得原位置颜色
		Color color = g.getColor();
		// 设置导弹颜色
		g.setColor(Color.ORANGE);
		g.fillOval(x, y, MISSILE_WIDTH, MISSILE_HEIGHT);
		// 将前景色还原
		g.setColor(color);
		// 导弹移动
		move();
	}

	/**
	 * 导弹移动
	 */
	private void move() {
		switch (direction) {
		case LEFT:
			x -= X_SPEED;
			break;
		case RIGHT:
			x += X_SPEED;
			break;
		case UP:
			y -= Y_SPEED;
			break;
		case DOWN:
			y += Y_SPEED;
			break;
		default:
			break;
		}

		// 导弹飞出主窗口之后将其消亡
		if (x < 0 || y < 0 || x > MainFrame.GAME_WIDTH
				|| y > MainFrame.GAME_HEIGHT) {
			setLive(false);
		}
	}

	/** 碰撞检测 */
	public Rectangle getRectangle() {
		return new Rectangle(x, y, MISSILE_WIDTH, MISSILE_HEIGHT);
	}

	/**
	 * 判断坦克是否被击中
	 * 
	 * @param tank
	 * @return
	 */
	public boolean hitTank(Tank tank) {
		if (isLive() && tank.isLive() && isAlliance() != tank.isAlliance()
				&& getRectangle().intersects(tank.getRectangle())) {
			tank.setLive(false);
			setLive(false);
			// 产生爆炸
			Exploder exploder = new Exploder(x, y, client);
			client.exploders.add(exploder);
			return true;
		}
		return false;
	}

	/**
	 * 多辆坦克判断是否被击中
	 * 
	 * @param tanks
	 * @return
	 */
	public boolean hitTanks(Set<Tank> tanks) {
		for (Tank enemyTank : tanks) {
			if (hitTank(enemyTank)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 导弹撞墙
	 * 
	 * @param wall
	 * @return
	 */
	public boolean hitWall(Wall wall) {
		if (live && getRectangle().intersects(wall.getRectangle())) {
			live = false;
			return true;
		}
		return false;
	}

	/**
	 * 导弹撞墙
	 * 
	 * @param wall
	 * @return
	 */
	public boolean hitWalls(List<Wall> walls) {
		for (Wall wall : walls) {
			if (hitWall(wall)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 导弹相撞
	 * 
	 * @param missiles
	 * @return
	 */
	public boolean hitMissiles(List<Missile> missiles) {
		for (Missile missile : missiles) {
			// 自己不能撞自己
			if (this != missile) {
				if (isLive() && missile.isLive()
						&& getRectangle().intersects(missile.getRectangle())) {
					setLive(false);
					missile.setLive(false);
					return true;
				}
			}
		}
		return false;
	}

	public boolean isLive() {
		return live;
	}

	public void setLive(boolean live) {
		this.live = live;
	}

	public boolean isAlliance() {
		return alliance;
	}

	public void setAlliance(boolean alliance) {
		this.alliance = alliance;
	}

}
